package cn.qidisoft.edu.hzjt.service.zyc.impl;

import cn.qidisoft.core.common.code.QdfRespCode;
import cn.qidisoft.core.common.exception.dao.DBException;
import cn.qidisoft.core.common.log.LoggerAdapter;
import cn.qidisoft.core.common.log.LoggerAdapterFactory;
import cn.qidisoft.core.common.pagination.Page;
import cn.qidisoft.core.common.pagination.PaginationBean;
import cn.qidisoft.edu.hzjt.dao.zyc.TeacherThesisMapper;
import cn.qidisoft.edu.hzjt.model.zyc.ScientificNotice;
import cn.qidisoft.edu.hzjt.model.zyc.TeacherThesis;
import cn.qidisoft.edu.hzjt.service.zyc.ITeacherThesisService;
import cn.qidisoft.edu.hzjt.utils.LogType;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TeacherThesisServiceImpl implements ITeacherThesisService{

    protected static final LoggerAdapter LOGGER = LoggerAdapterFactory
            .getLogger(LogType.HZJT_LOGGER.val);

    @Autowired
    TeacherThesisMapper teacherThesisMapper;

    @Override
    public List<TeacherThesis> queryList(TeacherThesis record) {
        return teacherThesisMapper.queryList(record);
    }

    @Override
    public PaginationBean<TeacherThesis> queryPageList(TeacherThesis data, Page page) {
        try {
            int c = teacherThesisMapper.queryPageListcount(data);
            int totalRecords = c;
            PaginationBean<TeacherThesis> pageHolder = new PaginationBean<>(page, totalRecords);
            List<TeacherThesis> resultList = teacherThesisMapper
                    .queryPageList(data,
                            new RowBounds((pageHolder.getCurrentPage() - 1) * pageHolder.getPageSize(),
                                    pageHolder.getPageSize()));
            pageHolder.setPageList(resultList);
            return pageHolder;
        } catch (Exception e) {
            LOGGER.error("queryPageList [{}] error," + this.getClass().getName() + "=[{}]",
                    this.getClass().getName(),
                    ToStringBuilder.reflectionToString(data, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                    e);
        }
    }

    @Override
    public TeacherThesis queryByPK(TeacherThesis record) {
        return teacherThesisMapper.queryByPK(record);
    }

    @Override
    public Integer insert(TeacherThesis record) {
        teacherThesisMapper.insert(record);
        if (StringUtils.isNotEmpty(record.getGid())){
            TeacherThesis nr = queryByPK(record);
            if (nr!=null&&nr.getLwid()!=null) {
                int newid = nr.getLwid();
                record.setLwid(newid);
                return newid;
            }
        }
        return null;
    }

    @Override
    public int update(TeacherThesis record) {
        teacherThesisMapper.update(record);
        return 0;
    }

    @Override
    public int delete(TeacherThesis record) {
        teacherThesisMapper.delete(record);
        return 0;
    }

    @Override
    public int validation(TeacherThesis record) {
        return teacherThesisMapper.validation(record);
    }

	@Override
	public List<TeacherThesis> getLwspList(Map<String, Object> queryMap) {
		
		return teacherThesisMapper.getLwspList(queryMap);
	}

	@Override
	public void updatetJJG(String[] data, String tJJG) {
		Map<String, Object> qMap = new HashMap<String,Object>();
		for(int i = 0; i < data.length; i++) {
			qMap.put("lWID", data[i]);
			qMap.put("tJJG", tJJG);
			teacherThesisMapper.updatetJJG(qMap);
		}
		
	}

	@Override
	public TeacherThesis getBylwid(String lwid) {
		return teacherThesisMapper.getBylwid(lwid);
	}

    @Override
    public PaginationBean<Map<String, Object>> queryUserAllAuditData(Map<String, Object> map,
                                                                     Page page) {
        try {
            int totalRecords = teacherThesisMapper.queryUserAllAuditDataCount(map);

            PaginationBean<Map<String, Object>> pageHolder = new PaginationBean<>(page,
                    totalRecords);
            List<Map<String, Object>> resultList = teacherThesisMapper.queryUserAllAuditData(map,
                    new RowBounds((pageHolder.getCurrentPage() - 1) * pageHolder.getPageSize(),
                            pageHolder.getPageSize()));
            pageHolder.setPageList(resultList);
            return pageHolder;
        } catch (Exception e) {
            LOGGER.error("queryPageList [{}] error," + this.getClass().getName() + "=[{}]",
                    this.getClass().getName(),
                    ToStringBuilder.reflectionToString(map, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                    e);
        }
    }


}
